package com.daqsoft.controller.project;

import com.daqsoft.data.User;
import com.daqsoft.data.project.Project;
import com.daqsoft.data.project.ProjectUser;
import com.daqsoft.dto.GeneralResultEnum;
import com.daqsoft.dto.StatusEnum;
import com.daqsoft.permission.Verify;
import com.daqsoft.permission.VerifyType;
import com.daqsoft.service.project.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 我的项目控制器
 * @author 周麟东 on 10:12
 */
@RestController
@RequestMapping("myProject")
public class MyProjectController {

    @Autowired
    private ProjectService service;

    @RequestMapping("getMyProject")
    @Verify(value = VerifyType.HAVE_PERMISSION,desc = "获取当前用的所有项目")
    public GeneralResultEnum getUserProject(HttpServletRequest request , Boolean isAllProject , Boolean showChange){
        User user = (User) request.getSession().getAttribute("user");
        List<Map<String,Object>> projects = new ArrayList<>();
        List<Project> projectList;
        if (isAllProject!= null && isAllProject){
            projectList = service.getAll();
        }else if(showChange!=null && showChange){
            projectList = service.getAllByPm(user);
        }else {
            projectList = service.getByPm(user);
        }
        projectList = projectList.stream().filter(project -> project.getStatus()!= StatusEnum.HIDDEN).collect(Collectors.toList());
        for (Project project : projectList) {
            projects.add(project.toMap());
        }
        return GeneralResultEnum.SUCCESS.setData(projects).setDesc("获取用户所有项目成功");
    }

    @RequestMapping("removeChangeProject")
    @Verify(value = VerifyType.HAVE_PERMISSION,desc = "删除最后一个正在变更的项目")
    public GeneralResultEnum removeChangeProject(int id){
        Project project = service.get(id);
        if (project == null){
            return GeneralResultEnum.FAIL.setDesc("该项目已经不存在");
        }
        if(service.removeChangeProject(project)){
            return GeneralResultEnum.SUCCESS;
        }else {
            return GeneralResultEnum.FAIL.setDesc("这个项目不能通过该方式删除");
        }
    }

}
